import {AnyAction, Reducer} from 'redux';

import {EffectsCommandMap} from 'dva';
import {BasicInfoArrayData, CustomerInfoData} from './data.d';
import {getCustomerInfo, fetch} from './service';

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: CustomerInfoData) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: CustomerInfoData;
  effects: {
    fetch: Effect;
    fetchCustomerInfo: Effect;
  };
  reducers: {
    save: Reducer<CustomerInfoData>;
    clear: Reducer<CustomerInfoData>;
    saveCustomerInfoData: Reducer<CustomerInfoData>;
  };
}

const initState = {
  customerInfo: {

  }
};

const Model: ModelType = {
  namespace: 'customerInformation',

  state: initState,

  effects: {
    * fetch(_, {call, put}) {
      const response = yield call(fetch);
      console.log(response);
      yield put({
        type: 'save',
        payload: response,
      });
    },

    * fetchCustomerInfo({payload}, {call, put}) {
      console.log(payload);
      const response = yield call(getCustomerInfo, payload);
      if (!response) return;
      const {data: {customerInfo = {}}} = response;
      console.log(response);
      yield put({
        type: 'saveCustomerInfoData',
        payload: {
          customerInfo,
        },
      });
    },
  },

  reducers: {
    save(state, {payload}) {
      return {
        ...state,
        ...payload,
      };
    },
    clear() {
      return initState;
    },
    saveCustomerInfoData(state, {payload}) {

      const {customerInfo} = payload;
      const personalInfo: BasicInfoArrayData[] = [];
      const contactInfo: BasicInfoArrayData[] = [];
      const locationInfo: BasicInfoArrayData[] = [];

      Object.assign(customerInfo, {personalInfo});
      Object.assign(customerInfo, {contactInfo});
      Object.assign(customerInfo, {locationInfo});

      const setArrayObject =
        (key: string, value: string, icon: string, title: string) =>
          ({key, value, icon, title});
      const setPersonalInfo =
        (key: string, value: string, icon: string, title: string) => {
          personalInfo.push(setArrayObject(key, value, icon, title))
        };
      const setContactInfo =
        (key: string, value: string, icon: string, title: string) => {
          contactInfo.push(setArrayObject(key, value, icon, title))
        };
      const setLocationInfo =
        (key: string, value: string, icon: string, title: string) => {
          locationInfo.push(setArrayObject(key, value, icon, title))
        };

      setPersonalInfo('birthday', customerInfo.birthday,
        'rws-birthday', 'Date of Birth');
      setPersonalInfo('langPreferences', customerInfo.langPreferences,
        'rws-language', 'Preferred Language');
      setPersonalInfo('passport', customerInfo.passport,
        'rws-certificate', 'Identification Document No.');
      setPersonalInfo('residentialStatus', customerInfo.residentialStatus,
        'rws-residence', 'Residential Status');
      setPersonalInfo('nationality', customerInfo.nationality,
        'rws-birthday', 'Nationality');

      setContactInfo('phone', customerInfo.phone,
        'rws-termination-forbid', 'Primary Contact');
      setContactInfo('email', customerInfo.email,
        'rws-message', 'Email');

      setLocationInfo('rmCode', customerInfo.rmCode,
        'rws-user', 'RM Code');
      setLocationInfo('currentGameTerminal', customerInfo.currentGameTerminal,
        'rws-location', 'Date of Birth');
      setLocationInfo('currentHotelStay', customerInfo.currentHotelStay,
        'rws-room', 'Date of Birth');

      return {
        ...state,
        customerInfo,
      };
    },
  },
};

export default Model;
